import QQMapWX from '@/common/qqmap-wx-jssdk.js';
import {
	mapState,
	mapMutations
} from 'vuex';
import Vue from 'vue';
class Location{
	constructor(arg) {
		
	}
	
	// ...mapMutations(['SET_LOCATION']),
	getAddress(e) {
		   return new Promise((resolve,reject)=>{
			   uni.authorize({
			   		scope: "scope.userLocation",
			   		success: (res) => {
						console.log(res)
			   			let qqmapsdk = new QQMapWX({
			   				// 填写自己的Key值，这个值是与AppID绑定的
			   				key: 'TXIBZ-KEVCM-QCU62-6753N-XPKOS-PZBEE'
			   			});
			   			uni.getLocation({
			   				type: 'gcj02',
			   				success: (res) => {
			   					qqmapsdk.reverseGeocoder({
			   						location: {
			   							latitude: res.latitude,
			   							longitude: res.longitude
			   						},
			   						success: (res) => {							
			   							res.name = res.result.ad_info.district;
			   							res.citycode = res.result.ad_info.adcode;
			   							res.province=res.result.ad_info.province;
			   							res.city=res.result.ad_info.city;
										Vue.prototype.$store.commit('SET_LOCATION',res)
			   						    resolve(res)
			   						},
			   						fail: (err) => {
										console.log(err,"shibai")
										uni.showModal({
											title:"获取用户定位失败，请手动选择当前位置",
											success:(Lres)=> {
												if(Lres.confirm){
													uni.chooseLocation({
													        success: (Lresult) => {
																console.log(Lresult,"Lresult")
																res.city=Lresult.name.substring(0,3)+"···";
																
																res.result={
																	location:{
																		lat:Lresult.latitude,
																		lng:Lresult.longitude
																	}
																};
																
																
																Vue.prototype.$store.commit('SET_LOCATION',res)
																resolve(res)
													        },
													        fail: (errs) => {
													          // 获取位置失败的处理
													          console.log(errs,'获取位置失败');
													        },
													  });
													  // reject(err)
												}
											}
										})
									
			   						}
			   					})
			   				},
							fail(err) {
								console.log(err,111)
							}
			   			})
			   		},fail:(err)=> {
						
						
			   			uni.showModal({
			   				content: '检测到您还没有打开位置授权，是否去设置打开？',
			   				confirmText: "确认",
			   				cancelText: "取消",
			   				success: function(res) {
			   					if (res.confirm) {
			   						uni.openSetting({
			   							success: (res) => {
			   			
			   							}
			   						})
			   					} else {
			   						console.log('用户点击取消')
			   					}
			   				}
			   			})
			   		}
			   })
		   })
	}
}

export default Location;